package com.conf;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Configuration;
import org.springframework.web.servlet.config.annotation.InterceptorRegistry;
import org.springframework.web.servlet.config.annotation.ResourceHandlerRegistry;
import org.springframework.web.servlet.config.annotation.WebMvcConfigurer;

@Configuration
public class WebConfig implements WebMvcConfigurer {

    @Autowired
    private AppInterceptor appInterceptor;

    //放行请求
    String[] excludePath = new String[]{
      "/user/pwdLogin","/user/smsLogin",
      "/user/getImgCaptcha","/user/getSmsCaptcha",
      "/favicon.ico",
      "/error",
      "/webjars/**",
      "/v3/api-docs",
      "/swagger-resources",
      "/doc.html",
      "/v2/api-docs",
      "/mode-Text.js"
    };

    @Override
    public void addInterceptors(InterceptorRegistry registry) {
        // 添加拦截器，并设置拦截规则
        registry.addInterceptor(appInterceptor)
                .addPathPatterns("/**") // 拦截所有请求
                .excludePathPatterns(excludePath); // 排除某些请求
    }

    //解决swagger访问404
    @Override
    public void addResourceHandlers(ResourceHandlerRegistry registry) {
        registry.addResourceHandler("doc.html","/mode-Text.js","swagger-ui.html")
                .addResourceLocations("classpath:/META-INF/resources/");
        //
        registry.addResourceHandler("/webjars/**")
                .addResourceLocations("classpath:/META-INF/resources/webjars/");
    }

}
